<?php

class Librarian_PublisherController extends Zend_Controller_Action
{

    public function indexAction()
    {
        $q = trim($this->getRequest()->getParam('q', ''));

        if ($q)
        {
            $publishers = Model_Publisher::find(array('q' => $q));
        }
        else
        {
            $publishers = Model_Publisher::getAll();
        }

        $this->view->assign('q', $q);
        $this->view->assign('publishers', $publishers);
    }

    public function createAction()
    {
        if ($this->getRequest()->isPost())
        {
            $response = array(
                'status'  => 0,
                'message' => 'Unknown error...'
            );

            $params = $this->getRequest()->getPost();

            if (!$params['Name'] || strlen($params['Name']) > 200)
            {
                $response['message'] = 'Tên chuyên mục phải từ 1 - 200 ký tự.';
            }
            else
            {
                try
                {
                    if (Model_Publisher::create($params))
                    {
                        $response['status']  = 1;
                        $response['message'] = 'Success';
                    }
                }
                catch (Zend_Db_Exception $e)
                {
                    $response['message'] = $e->getMessage();
                }
            }

            $this->_helper->json($response);
        }
    }

    public function editAction()
    {
        if ($this->getRequest()->isPost())
        {

            $response = array(
                'status'  => 0,
                'message' => 'Unknown error...'
            );

            $params = $this->getRequest()->getPost();

            if (!$params['Name'] || strlen($params['Name']) > 200)
            {

                $response['message'] = 'Tên chuyên mục phải từ 1 - 200 ký tự.';
            }
            elseif (!$params['PublisherID'])
            {
                $response['message'] = 'Bạn chưa chọn nhà xuất bản để chính sửa.';
            }
            else
            {

                try
                {

                    Model_Publisher::edit($params);
                    $response['status']  = 1;
                    $response['message'] = 'Success';
                }
                catch (Zend_Db_Exception $e)
                {
                    $response['message'] = $e->getMessage();
                }
            }

            $this->_helper->json($response);
        }

        $params = $this->getRequest()->getParams();

        if (!intval($params['publisher_id']))
        {
            $this->_redirect('/librarian/publisher');
        }

        $publisher = Model_Publisher::get($params);

        if (empty($publisher))
        {
            $this->_redirect('/librarian/publisher');
        }

        $this->view->assign('publisher', $publisher);
    }

    public function deleteAction()
    {
        if ($this->getRequest()->isPost())
        {

            $response = array(
                'status'  => 0,
                'message' => 'Unknown error...'
            );

            $params = $this->getRequest()->getPost();

            if (!intval($params['publisherID']))
            {

                $response['message'] = 'Bạn chưa chọn nhà xuất bản để xoá.';
            }
            else
            {
                try
                {

                    if (Model_Publisher::delete($params))
                    {
                        $response['status']  = 1;
                        $response['message'] = 'Success';
                    }
                }
                catch (Zend_Db_Exception $e)
                {
                    $response['message'] = $e->getMessage();
                }
            }

            $this->_helper->json($response);
        }
    }

}
